package com.jiangjiang.eshop.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangjiang.eshop.auth.dto.RoleDTO;
import com.jiangjiang.eshop.auth.dto.RoleQueryPageDTO;
import com.jiangjiang.eshop.auth.dto.RoleSaveDTO;
import com.jiangjiang.eshop.auth.dto.RoleUpdateDTO;
import com.jiangjiang.eshop.auth.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName RoleController
 * @Description 角色管理模块
 * @Author jhw
 * @Date 2021/6/16 10:43
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/auth/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 角色管理-分页查询
     *
     * @param dto 分页查询dto
     * @return 查询结果
     */
    @PostMapping("/queryForPage")
    public Page<RoleDTO> queryForPage(@RequestBody RoleQueryPageDTO dto) {
        try {
            return roleService.queryForPage(dto);
        } catch (Exception e) {
            log.warn("RoleController queryForPage error", e);
        }
        return null;
    }

    /**
     * 根据id查询角色
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public RoleDTO queryById(@PathVariable("id") Long id) {
        try {
            return roleService.queryById(id);
        } catch (Exception e) {
            log.warn("RoleController queryById error", e);
        }
        return null;
    }

    @PostMapping("/save")
    public Boolean saveRole(@RequestBody RoleSaveDTO dto){
        try {
            return roleService.saveRole(dto);
        } catch (Exception e) {
            log.warn("RoleController queryById error", e);
        }
        return false;
    }

    /**
     * 更新角色
     * @param dto 更新角色 DTO
     * @return 处理结果
     */
    @PostMapping("/update")
    public Boolean updateRole(@RequestBody RoleUpdateDTO dto){
        try {
            return roleService.updateRole(dto);
        } catch (Exception e) {
            log.warn("RoleController updateRole error", e);
        }
        return false;
    }

    /**
     * 更新角色
     * @param dto 更新角色 DTO
     * @return 处理结果
     */
    @GetMapping("/delete/{id}")
    public Boolean deleteRole(@PathVariable("id") Long id){
        try {
            return roleService.deleteRole(id);
        } catch (Exception e) {
            log.warn("RoleController deleteRole error", e);
        }
        return false;
    }
}